package com.jwater.core.manager.worker.assign;

import com.jwater.core.manager.common.model.WorkerInfo;
import com.jwater.launch.DeployPathUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.util.Map;

/**
 * @description:
 * @author: jnner
 * @createDate: 2021/3/27
 * @version: 1.0
 */
public class AssignProp {
    protected static Logger logger = LoggerFactory.getLogger(AssignProp.class);

    public static void assign(WorkerInfo workerInfo) {
        Long version = workerInfo.getInfoProps().getLong("version");
        File deployPath = DeployPathUtils.getDeployPath(workerInfo.getRegionName(), workerInfo.getDeployName(), version);
        System.setProperty("JWATER_DEPLOY_PATH", deployPath.getPath());
        WorkerEnv.getInstance().put("JWATER_DEPLOY_PATH", deployPath.getPath());
        logger.info("prop=>JWATER_DEPLOY_PATH=" + deployPath.getPath());

        for (Map.Entry<String, Object> en : workerInfo.getProps().entrySet()) {
            if (en.getKey().startsWith("prop.")) {
                String key = en.getKey().replace("prop.", "");
                System.setProperty(key, en.getValue() + "");
                WorkerEnv.getInstance().put(key, en.getValue() + "");
                logger.info("prop=>" + key + "=" + en.getValue());
            } else if (en.getKey().startsWith("env.")) {
                String key = en.getKey().replace("env.", "");
                WorkerEnv.getInstance().put(key, System.getenv(key));
                logger.info("env=>" + key + "=" + System.getenv(key));
            }
        }
    }
}
